package com.atshiyou.web;

import com.atshiyou.bean.Student;
import com.atshiyou.service.impl.StudentDaoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/alterstudent")
public class AlterStuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String stuid2 = req.getParameter("stuid");
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String stuage2 = req.getParameter("stuage");
        String gradeid2 = req.getParameter("gradeid");
        int stuid = Integer.parseInt(stuid2);
        int stuage = Integer.parseInt(stuage2);
        int gradeid = Integer.parseInt(gradeid2);
        //2.调取service
        StudentDaoServiceImpl studentDaoService = new StudentDaoServiceImpl();
        Student student = new Student(stuid, stuno, stuname, stuage, gradeid);
        boolean b = studentDaoService.alterStudent(stuid, student);
        if(b){
            PrintWriter writer = resp.getWriter();
            writer.write("<script>alert('修改学生信息成功!');location.href='./index.jsp'</script>");
        }else{
            PrintWriter writer = resp.getWriter();
            writer.write("<script>alert('修改学生信息失败!');location.href='./index.jsp'</script>");
        }

    }
}
